/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.pucp.cpnc.mm.view;

/**
 *
 * @author Usuario
 */

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import pe.edu.pucp.cpnc.mm.manager.CmdManager;
import pe.edu.pucp.cpnc.mm.renderer.CameraPanController;

public class MainEventHandler implements MouseListener, KeyListener, FocusListener, MouseWheelListener {
    private final MapViewerPanel workingPanel;
    private double xReal=0;
    private double yReal=0;
    private double xRealOld = 0;
    private double yRealOld = 0;
    private boolean mouseRClickPressed = false;
    
    public MainEventHandler(MapViewerPanel workingPanel){
        this.workingPanel = workingPanel;
    }
    
    public boolean mouseRClickPressed(){
        return mouseRClickPressed;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
       
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON3){
            xRealOld=(double)e.getX();
            yRealOld=(double)e.getY();
        }
        
        if(e.getButton() == MouseEvent.BUTTON1){
            mouseRClickPressed = true;
            
        }
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        xReal=(double)e.getX();
        yReal=(double)e.getY();
        
        if(!workingPanel.isFocusOwner()) workingPanel.requestFocus();
        
        //Add an object
        if(e.getButton() == MouseEvent.BUTTON1){ 
            mouseRClickPressed = false;
            if(MainFrame.selectedTile!=null)
                CmdManager.svm.pushState(workingPanel.getMap());
            
            int j = MainFrame.getIsoX(xReal, yReal);
            int i = MainFrame.getIsoY(xReal, yReal);
           
            if((MainFrame.selObj != null) && (MainFrame.selectedPlayer!=null))
                CmdManager.addObject(j, i, MainFrame.selObj,
                        workingPanel.getMap()
                        ,MainFrame.selectedPlayer.getId() 
                        ,MainFrame.selectedOrientation);
        }
        
        if(e.getButton() == MouseEvent.BUTTON3){
            MainFrame.selObj=null;
            MainFrame.selectedTile=null;
        }
        
        //Move an object
        if(e.getButton() == MouseEvent.BUTTON3 && ((xReal != xRealOld) || (yReal != yRealOld)) ){
            int j = MainFrame.getIsoX(xReal, yReal);
            int i = MainFrame.getIsoY(xReal, yReal);
            int j2 = MainFrame.getIsoX(xRealOld, yRealOld);
            int i2 = MainFrame.getIsoY(xRealOld, yRealOld);
            
            CmdManager.updateObject(j2, i2, j, i,workingPanel.getMap());
        }
        
        //Erase an object
        if(e.getButton() == MouseEvent.BUTTON2){ 
            
            int j = MainFrame.getIsoX(xReal, yReal);
            int i = MainFrame.getIsoY(xReal, yReal);
            CmdManager.eraseObject(j, i,workingPanel.getMap());
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch(e.getKeyCode()){
            case(KeyEvent.VK_DOWN): 
                CameraPanController.panOn(CameraPanController.Direction.DOWN);
                break;
            case(KeyEvent.VK_UP): 
                CameraPanController.panOn(CameraPanController.Direction.UP);
                break;
            case(KeyEvent.VK_LEFT): 
                CameraPanController.panOn(CameraPanController.Direction.LEFT);
                break;
            case(KeyEvent.VK_RIGHT): 
                CameraPanController.panOn(CameraPanController.Direction.RIGHT);
                break;
            case(KeyEvent.VK_ESCAPE):
                MainFrame.selObj=null;
                MainFrame.selectedTile=null;
        }    
    }

    @Override
    public void keyReleased(KeyEvent e) {
        switch(e.getKeyCode()){
            case(KeyEvent.VK_DOWN): 
                CameraPanController.panOff(CameraPanController.Direction.DOWN);
                break;
            case(KeyEvent.VK_UP): 
                CameraPanController.panOff(CameraPanController.Direction.UP);
                break;
            case(KeyEvent.VK_LEFT): 
                CameraPanController.panOff(CameraPanController.Direction.LEFT);
                break;
            case(KeyEvent.VK_RIGHT): 
                CameraPanController.panOff(CameraPanController.Direction.RIGHT);
                break;
            case(KeyEvent.VK_ESCAPE):
                MainFrame.selObj=null;
                MainFrame.selectedTile=null;
        }
    }

    @Override
    public void focusGained(FocusEvent e) {
    }

    @Override
    public void focusLost(FocusEvent e) {
        CameraPanController.panOff(CameraPanController.Direction.DOWN);
        CameraPanController.panOff(CameraPanController.Direction.UP);
        CameraPanController.panOff(CameraPanController.Direction.LEFT);
        CameraPanController.panOff(CameraPanController.Direction.RIGHT);
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        int j = MainFrame.getIsoX(e.getX(), e.getY());
        int i = MainFrame.getIsoY(e.getX(), e.getY());
        
        if(e.getWheelRotation()>0){
            int result = CmdManager.rotateObject(j, i, 1, workingPanel.getMap());
            //if there was no object to rotate
            if(result==1 && MainFrame.selObj!=null) 
                MainFrame.selectedOrientation = (MainFrame.selectedOrientation+1) 
                % MainFrame.selObj.getSprite().size();
        }else{
            int result = CmdManager.rotateObject(j, i, 2, workingPanel.getMap());
            //if there was no object to rotate
            if(result==1 && MainFrame.selObj!=null){
                MainFrame.selectedOrientation--;
                MainFrame.selectedOrientation = MainFrame.selectedOrientation<0 ? 
                        MainFrame.selectedOrientation=MainFrame.selObj.getSprite().size()-1 : 
                        MainFrame.selectedOrientation;
            }
        }
    }
}
